<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/3
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\common\controller\Api;

use think\Db;

use think\Config;

/**
 * 公共无需登陆接口
 * @ApiInternal()
 */
class Tpmecmscom extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

  
    /**
     * 可在继承app\common\controller\Tpmecms 下面的里面直接使用该控制器里面的方法 使用方法：$this->p_tpmecsm();
     *
     * @return void
     */
    protected function p_tpmecms()
    {
        $this->success('TpMeCms', ['com' => 'TpMeCMS']);
    }

    /**
     * 是否可以发布空间
     *
     * @return void
     */
    protected function IsAddZone(){
        if($this->auth->verify_status!='2'){
            $this->error('未通过审核');
        }
    }

    /**
     * 是否可以发布提问
     *
     * @return void
     */
    protected function IsAddAsk(){
        if($this->auth->verify_status!='2'){
            $this->error('未通过审核');
        }
    }

    /**
     * 是否可以发布活动
     *
     * @return void
     */
    protected function IsAddAct(){
        if($this->auth->verify_status!='2'){
            $this->error('未通过审核');
        }
    }


    /**
     * 获取用户群组名
     *
     * @param int $uid 用户id
     * @param int $re_uid 用户id
     * @return void
     */
    protected function GetUserGroupName($uid,$re_uid)
    {
        
        if($uid > $re_uid){
            $group = 'im.room.user'.$uid.'_'.$re_uid;
        }else{
            $group = 'im.room.user'.$re_uid.'_'.$uid;
        }
        return $group;
    }
    /**
     * 获取空间评论数组
     */
    protected function GetZoneCommentArray($zone_id){
        $list = Db::name('zone_comment')->where('zone_id',$zone_id)->where(function($zone){
            $zone->whereOr('comment_id','<',1);
            $zone->whereOr('comment_id','null');
        })->order('time desc')->select();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
            $val['comment'] = $this->GetZoneComment2Array($zone_id,$val['id']);// Db::name('zone_comment')->where('zone_id',$zone_id)->where('comment_id',$val['id'])->order('time desc')->select();
        }
        return $list;
    }

    /**
     * 获取提问评论数组
     */
    protected function GetAskCommentArray($ask_id){
        $list = Db::name('ask_comment')
                    ->where('ask_id',$ask_id)
                    ->where(function($zone){
                        $zone->whereOr('comment_id','<',1);
                        $zone->whereOr('comment_id','null');
                    })
                    ->order('time desc')
                    ->select();
        foreach ($list as $key => &$val) {
            $val['comment_num'] = Db::name('ask_comment')
                                ->where('ask_id',$ask_id)
                                ->where('comment_id',$val['id'])
                                ->count();//评论数

            $val['likes'] = Db::name('asklikes')
                                ->where('ask_id',$ask_id)
                                ->where('comment_id',$val['id'])
                                ->count();//点赞数
            $val['self_likes'] = $this->GetUserIsAsklikes($ask_id,$this->auth->id,$val['id']);

            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
            $val['comment'] = $this->GetAskComment2Array($ask_id,$val['id']);// Db::name('ask_comment')->where('ask_id',$ask_id)->where('comment_id',$val['id'])->order('time desc')->select();
        }
        return $list;
    }

    /**
     * 获取活动评论数组
     */
    protected function GetActCommentArray($act_id){
        $list = Db::name('act_comment')
                    ->where('act_id',$act_id)
                    ->where(function($zone){
                        $zone->whereOr('comment_id','<',1);
                        $zone->whereOr('comment_id','null');
                    })
                    ->order('time desc')
                    ->select();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
            $val['comment'] = $this->GetActComment2Array($act_id,$val['id']);// Db::name('act_comment')->where('act_id',$act_id)->where('comment_id',$val['id'])->order('time desc')->select();
        }
        return $list;
    }

    /**
     * 获取用户是否点赞动态
     *
     * @param int $zone_id
     * @param int $uid
     * @return void
     */
    protected function GetUserIsZonelikes($zone_id,$uid)
    {
        $res = Db::name('zonelikes')->where('uid',$uid)->where('zone_id',$zone_id)->find();
        if($res){
            return true;
        }
        return false;
    }

    /**
     * 获取用户是否点赞提问
     *
     * @param int $ask_id
     * @param int $uid
     * @return void
     */
    protected function GetUserIsAsklikes($ask_id,$uid,$comment_id=null)
    {
        $where['uid'] = $uid;
        $where['ask_id'] = $ask_id;
        if($comment_id)
            $where['comment_id'] = $comment_id;

        $res = Db::name('asklikes')
            ->where($where)
            // ->where('ask_id',$ask_id)
            ->find();
        if($res){
            return true;
        }
        return false;
    }


    /**
     * 获取用户是否点赞活动
     *
     * @param int $ask_id
     * @param int $uid
     * @return void
     */
    protected function GetUserIsActlikes($act_id,$uid)
    {
        $res = Db::name('actlikes')->where('uid',$uid)->where('act_id',$act_id)->find();
        if($res){
            return true;
        }
        return false;
    }


     /**
     * 获取空间评论数组
     */
    protected function GetZoneComment2Array($zone_id,$comment_id){

        $list = Db::name('zone_comment')
                ->where('zone_id',$zone_id)
                // ->where('comment_id','>',0)
                ->where('top_id',$comment_id)
                // ->where('top_status','2')
                ->order('time asc')
                ->select();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
         
            if($val['comment_uid']==''||$val['comment_uid']==null)
                $val['comment_uid'] = Db::name('zone_comment')->where('id',$val['comment_id'])->value('uid');

            $val['nickname'] = Db::name('user')->where('id',$val['comment_uid'])->value('nickname');
            // $val['comment'] = Db::name('zone_comment')->where('zone_id',$zone_id)->where('comment_id','<',$val['id'])->order('time desc')->select();
        }

        // $list = Db::name('zone_comment')->where('zone_id',$zone_id)->where('comment_id',$comment_id)->order('time desc')->select();
        // foreach ($list as $key => &$val) {
        //     $val['date'] = $this->TimeToStr($val['time']);
        //     $val['user'] = $this->GetUserData2($val['uid']);
        //     // $val['comment'] = Db::name('zone_comment')->where('zone_id',$zone_id)->where('comment_id','<',$val['id'])->order('time desc')->select();
        // }

        return $list;
    }

    /**
     * 获取提问评论数组
     */
    protected function GetAskComment2Array($ask_id,$comment_id){

        $list = Db::name('ask_comment')
                ->where('ask_id',$ask_id)
                // ->where('comment_id',$comment_id)
                ->where('top_id',$comment_id)
                ->order('time desc')
                ->select();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);

            // $val['comment_num'] = Db::name('ask_comment')->where('comment_id',$val['id'])->count();
            // $val['likes_num'] = Db::name('asklikes')->where('comment_id',$val['id'])->count();
            $val['self_likes'] = $this->GetUserIsAsklikes($ask_id,$this->auth->id,$val['id']);

            // $val['comment'] = Db::name('ask_comment')->where('ask_id',$ask_id)->where('comment_id','<',$val['id'])->order('time desc')->select();
            if($val['comment_uid']==''||$val['comment_uid']==null)
                $val['comment_uid'] = Db::name('ask_comment')->where('id',$val['comment_id'])->value('uid');
                $val['nickname'] = Db::name('user')->where('id',$val['comment_uid'])->value('nickname');
        }
        return $list;
    }

    /**
     * 获取活动评论数组
     */
    protected function GetActComment2Array($act_id,$comment_id){

        $list = Db::name('act_comment')
                ->where('act_id',$act_id)
                // ->where('comment_id',$comment_id)
                ->where('top_id',$comment_id)
                ->order('time desc')
                ->select();
        foreach ($list as $key => &$val) {
            $val['date'] = $this->TimeToStr($val['time']);
            $val['user'] = $this->GetUserData2($val['uid']);
            // $val['comment'] = Db::name('act_comment')->where('act_id',$act_id)->where('comment_id','<',$val['id'])->order('time desc')->select();
            if($val['comment_uid']==''||$val['comment_uid']==null)
                $val['comment_uid'] = Db::name('act_comment')->where('id',$val['comment_id'])->value('uid');
                $val['nickname'] = Db::name('user')->where('id',$val['comment_uid'])->value('nickname');
        }
        return $list;
    }

    /**
     * 删除消息
     *
     * @param int $status 类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息,8=关注
     * @param int $public_id 公共ID
     * @return void
     */
    protected function DelMsg($status=null,$public_id=null)
    {
        $data['status'] = $status;
        $data['public_id'] = $public_id;
        Db::name('msg')->where($data)->delete();
    }

    /**
     * 删除动态评论By动态id
     *
     * @param int $id 动态
     * @return void
     */
    protected function DelZoneCommentByzone_id($id){
        $ids = Db::name('zone_comment')->where('zone_id',$id)->column('id');
        if($ids){
            Db::name('zone_comment')->where('comment_id','in',$ids)->delete();
        }
        Db::name('zone_comment')->where('zone_id',$id)->delete();
        //类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息,8=关注
        $this->DelMsg(1,$id);
        $this->DelMsg(2,$id);
    }

    /**
     * 删除提问评论By动态id
     *
     * @param int $id 提问
     * @return void
     */
    protected function DelAskCommentByask_id($id){
        $ids = Db::name('ask_comment')->where('ask_id',$id)->column('id');
        if($ids){
            Db::name('ask_comment')->where('comment_id','in',$ids)->delete();
        }
        Db::name('ask_comment')->where('ask_id',$id)->delete();
        //类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息,8=关注
        $this->DelMsg(3,$id);
        $this->DelMsg(4,$id);

    }


    /**
     * 删除活动评论By动态id
     *
     * @param int $id 活动
     * @return void
     */
    protected function DelActCommentByact_id($id){
        $ids = Db::name('act_comment')->where('act_id',$id)->column('id');
        if($ids){
            Db::name('act_comment')->where('comment_id','in',$ids)->delete();
        }
        Db::name('act_comment')->where('act_id',$id)->delete();
         //类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息,8=关注
        $this->DelMsg(5,$id);
        $this->DelMsg(6,$id);
    }

    /**
     * 获取个性标签数组
     *
     * @param [type] $ids
     * @return void
     */
    protected function GetCharacterIdsArray($ids)
    {
        if($ids)
        return Db::name('character')->where('id','in',$ids)->column('name');
    }


    protected function Verifyact_id($id)
    {
        $act = Db::name('act')->where('id',$id)->find();
        if(!$act){
            $this->error('该活动不存在');
        }
        return $act;
    }


    /**
     * 验证文字
     *
     * @param string $str 文字
     * @return void
     */
    protected function VerifyStr($str=null)
    {
        $app = $this->get_app();
        $result = $app->content_security->checkText($str);
        if($result['errcode']!='0'){
            $this->error('含有违禁词');
        }
        if(Config::get('site.own_ban_switch')==1){
            $list = Db::name('ban_word')->select();
            if($list){
                foreach ($list as $key => $ban) {
                    if(strpos($str,$ban['name']) !== false){ 
                        $this->error('违禁词：'.$ban['name']);
                    }
                }
            }
        }
        
        
    }




    /**
     * 获取用户信息
     *
     * @return void
     */
    protected function GetUserData2($id)
    {
        
        $user = Db::name('user')->where('id',$id)->field('nickname,avatar,school_id,sex')->find();
        $user['school'] = Db::name('school')->where('id',$user['school_id'])->value('name');
        return $user;
    }


    /**
     * 获取粉丝信息
     *
     * @param [type] $id 用户id
     * @return void
     */
    protected function GetFansUserData($id)
    {
        return Db::name('user')->where('id',$id)->field('nickname,avatar')->find();
    }

    /**
     * 获取空间评论数
     *
     * @param [type] $zone_id
     * @return void
     */
    protected function GetZoneCommentNum($zone_id)
    {
        return Db::name('zone_comment')->where('zone_id',$zone_id)->count();
    }

    /**
     * 获取提问评论数
     *
     * @param [type] $ask_id
     * @return void
     */
    protected function GetAskCommentNum($ask_id)
    {
        return Db::name('ask_comment')->where('ask_id',$ask_id)->whereNull('top_id')->count();
    }

    /**
     * 获取活动评论数
     *
     * @param [type] $act_id
     * @return void
     */
    protected function GetActCommentNum($act_id)
    {
        return Db::name('act_comment')->where('act_id',$act_id)->count();
    }

    /**
     * 获取我是否关注了某用户
     *
     * @param [type] $uid 用户id
     * @return void
     */
    protected function GetMyIsfollowUser($uid)
    {
        return Db::name('fans')->where('uid',$uid)->where('fans_uid',$this->auth->id)->find();
    }


    
    /**
     * 获取对方是否否关注了我
     *
     * @param [type] $uid 对方用户id
     * @return void
     */
    protected function GetUserIsfollowMy($uid)
    {
        return Db::name('fans')->where('fans_uid',$uid)->where('uid',$this->auth->id)->find();
    }


    /**
     * 减动态点赞
     *
     * @param int $zone_id 动态id
     * @return void
     */
    protected function DecZoneLikes($zone_id){
        Db::name('zone')->where('id',$zone_id)->setDec('likes');
    }

    /**
     * 加动态点赞
     *
     * @param int $zone_id 动态id
     * @return void
     */
    protected function IncZoneLikes($zone_id){
        Db::name('zone')->where('id',$zone_id)->setInc('likes');
    }
   

    /**
     * 加动态浏览量
     *
     * @param int $zone_id 动态id
     * @return void
     */
    protected function IncZoneViews($zone_id){
        Db::name('zone')->where('id',$zone_id)->setInc('views');
    }



    /**
     * 减提问点赞
     *
     * @param int $ask_id 提问id
     * @return void
     */
    protected function DecAskLikes($ask_id){
        Db::name('ask')->where('id',$ask_id)->setDec('likes');
    }


    /**
     * 加提问点赞
     *
     * @param int $ask_id 提问id
     * @return void
     */
    protected function IncAskLikes($ask_id){
        Db::name('ask')->where('id',$ask_id)->setInc('likes');
    }



    /**
     * 加活动点赞
     *
     * @param int $act_id 活动id
     * @return void
     */
    protected function IncActLikes($act_id){
        Db::name('act')->where('id',$act_id)->setInc('likes');
    }



    /**
     * 减活动点赞
     *
     * @param int $act_id 活动id
     * @return void
     */
    protected function DecActLikes($act_id){
        Db::name('act')->where('id',$act_id)->setDec('likes');
    }




    /**
     * 加提问浏览量
     *
     * @param int $ask_id 提问id
     * @return void
     */
    protected function IncAskViews($ask_id){
        Db::name('ask')->where('id',$ask_id)->setInc('views');
    }
    

    /**
     * 加活动浏览量
     *
     * @param int $act_id 提问id
     * @return void
     */
    protected function IncActViews($act_id){
        Db::name('act')->where('id',$act_id)->setInc('views');
    }


    protected function IncMatchLog($uid,$user_id,$character_ids=null,$user_character_ids=null)
    {
        $data['uid'] = $uid;
        $data['user_id'] = $user_id;

        $data['date'] = date('Y-m-d');
        $data['time'] = time();
        if($character_ids)
        $data['character_ids'] = $character_ids;
        if($user_character_ids)
        $data['user_character_ids'] = $user_character_ids;
        Db::name('match')->insert($data);
    }

    /**
     * 用户今天是否可以匹配
     *
     * @param [type] $uid
     * @return void
     */
    protected function UserTodayIsMatch($uid)
    {
        // $data['uid'] = $uid;
        // $data['date'] = date('Y-m-d');
        $num = $this->GetUserTodayMatchNum($uid);//Db::name('match')->where($data)->count();
        $match_num = Config::get('site.everyday_match_num');
        if($num >= $match_num){
            $this->error('今日次数已用完');
        }
    }

    /**
     * 获取用户的今日匹配次数
     *
     * @param [type] $uid
     * @return void
     */
    protected function GetUserTodayMatchNum($uid)
    {
        $data['uid'] = $uid;
        $data['date'] = date('Y-m-d');
        return Db::name('match')->where($data)->count();
      
    }


    /**
     * 加消息
     *
     * @param int $uid 用户ID
     * @param int $status 类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息,8=关注用户
     * @param int $public_id 公共id
     * @param int $user_id 对方用户ID
     * @return void
     */
    protected function AddMsg($uid,$status,$public_id,$user_id=null,$title=null)
    {

        $data['public_id'] = $public_id;
        $data['uid'] = $uid;
        $data['status'] = $status;//类型:1=动态点赞,2=动态评论,3=提问点赞,4=提问评论,5=活动点赞,6=活动留言,7=聊天消息
        $data['user_id'] = $user_id;//对方ID
        $data['title'] = $title;
        switch ($status) {
            case '1':
                // $title = Db::name('')
                break;
            
            default:
                # code...
                break;
        }
        $data['time'] = time();
        Db::name('msg')->insert($data);
    }




    /**
     * 时间文字
     *
     * @param int $time 时间戳
     * @return void
     */
    protected function TimeToStr($time)
    {
        $xh = 6;
        $now_time = time();

        $today = false;//当天
        $now_date = date('Y-m-d');//现在日期
        $date = date('Y-m-d',$time);
        if($now_date==$date) 
            $today = true;

        if($time > ($now_time-60) && $time<$now_time){
            $str = '刚刚';
        }elseif($time > ($now_time-3600) ){
            $str = strval(intval(($now_time-$time)/60)).'分钟前';
        }elseif($time > ($now_time-3600*$xh) ){
            $str = strval(intval(($now_time-$time)/3600)).'小时前';
        }elseif($today){
            $str = date('H:i',$time);
        }else{
            $str = date('Y-m-d',$time);
        }

        return $str;
    }



}
